﻿using System;
using BrainTechLLC.ImageOperators;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Drawing;
using System.Threading;
using BrainTechLLC.WebCamLib3;

namespace WpfImageReceiver
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public VideoFeedProvider _feedProvider;
        public IVideoFeed _videoFeed;
        protected CameraHandler _cameraHandler;
        public bool _communicationsInitialized;

        public static readonly DependencyProperty CommunicationsProperty =
            DependencyProperty.Register("Communications", typeof(Communications), typeof(Window1), new PropertyMetadata(null, OnCommunicationsPropertyChanged));

        public Communications Communications
        {
            get { return (Communications)GetValue(CommunicationsProperty); }
            set { SetValue(CommunicationsProperty, value); }
        }

        private static void OnCommunicationsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            Window1 source = d as Window1;
            Communications vNew = e.NewValue as Communications;
            Communications vOld = e.OldValue as Communications;
            source.UpdateCommunications(vNew, vOld);
        }

        private void UpdateCommunications(Communications viewModel, Communications viewModelOld)
        {
            if (viewModel == viewModelOld)
                return;

            if (viewModelOld != null)
            {
                viewModelOld.PropertyChanged -= Communications_PropertyChanged;
            }

            Communications = viewModel;
            Communications.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Communications_PropertyChanged);

            // Let the UI bind to the view-model.
            this.DataContext = Communications;
        }

        void Communications_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Connected" && Communications.Connected)
            {
                var feed = new GenericVideoFeed();
                _cameraHandler._videoFeed = feed;
                feed.StartCameraCapture();
                _feedProvider = new VideoFeedProvider(Communications, feed);
                _videoFeed = feed;
                cameraControl.BeginProcessing();
            }
        }


        public Window1()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(Window1_Loaded);
            this.Topmost = true;
            _cameraHandler = new CameraHandler();
            cameraControl.cameraHandler = _cameraHandler;
        }

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            Communications = new Communications();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (!Communications.IsConnected())
            {
                Thread.Sleep(1000);
                Communications.TryConnect();
            }
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            if (!_communicationsInitialized)
            {
                _communicationsInitialized = true;
                Communications.Startup(true);

                DispatcherTimer timer = new DispatcherTimer(DispatcherPriority.Normal)
                {
                    Interval = new TimeSpan(0, 0, 0, 0, 50),
                };

                timer.Tick += new EventHandler(timer_Tick);
                timer.Start();                
            }
        }

        private void closeCommand_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
    }
}
